package edu.faytechcc.csc251.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;


/**
 *
 * @author woodsk9710
 */
public class SearchPanel extends JPanel
{
    private JTextField searchText;
    private JButton searchButton;
    public SearchPanel()
    {
        this.setBorder(BorderFactory.createTitledBorder("Search"));
        searchText = new JTextField(20);
        this.add(searchText);
        searchButton = new JButton("Search");
        this.add(searchButton);
    }

    /**
     * @return the searchText
     */
    public String getSearchText()
    {
        return searchText.getText();
    }

    public void addActionListenerToSearchButton(SearchButtonListener listener)
    {
        searchButton.addActionListener(listener);
    }
}
class SearchButtonListener implements ActionListener
{
    private SearchPanel searchPanel;
    private ResultsPanel resultsPanel;
    private ArrayList<String[]> data;
//    private String[] colNames;
    private ArrayList<String[]> newData;
    
    public SearchButtonListener(SearchPanel sPanel, ResultsPanel rPanel)
    {
        this.resultsPanel = rPanel;
        this.searchPanel = sPanel;
    }
    @Override
    public void actionPerformed(ActionEvent ae)
    {
        String searchText = searchPanel.getSearchText();
        data = new ArrayList<String[]>();
        data = resultsPanel.getModelList();

        for(String[] row : data) 
        {
            for(String col : row)
            {
                if(col.contains(searchText))
                {
                    newData.add(row);
                    break;
                }
            }
        }
        resultsPanel.setModelList(newData);
        
//        colNames = resultsPanel.getModelColumns();
//        int colCounter = 0;
//        int rowCounter = 0;
//        String[] currentArray;
//        String currentStr;        
//        for (int row = 0; row < data.size(); row++)
//        {
//            currentArray = data.get(row);
//            boolean check = false;
//            for(int col = 0; !check || col < colNames.length ; col++)
//                {
//                    currentStr = currentArray[col];
//                    check = currentStr.contains(searchText);
//                }   
//            if (check)
//            {
//                newData.add(currentArray);
//            }
//        }
    }
}
